(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var list = "";
	var currentPage = 1;
	var pageSize = 10;
	var totalSize;
	var now = 0;
	var dataCache = [];
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			initbtns();
			initGradeClass();
		});
	});
	
	function initGradeClass(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
				cmd:"getGradeAndClass",
				model:"tclass",
				userKey:userKey,
				data:{
					schoolcode:schoolcode
				},
				remark:"",
		};
		AjaxCrud.get2(serverUrl.ServerLoginUrl,data,function(res){
			var typeOpt = "<option value='' >学段</option>";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data;
				for(var i = 0; i < data.length; i++){
					var name = "";
					if(data[i].type == "0"){
						name = "幼儿园";
					}else if(data[i].type == "1"){
						name = "小学";
					}else if(data[i].type == "2"){
						name = "中学";
					}else if(data[i].type == "3"){
						name = "高中";
					}
					typeOpt += "<option value='"+data[i].type+"'>"+name+"</option>";
				}
				$("#student-periodtype").html(typeOpt);
				
				$("#student-periodtype").unbind("change");
				$("#student-periodtype").change(function(){
					var gradeData = null;
					var type = $("#student-periodtype option:selected").val();
					var gradeOpt = "<option value='' classcodes='' classes=''>年级</option>";
					if(type != ""){
						for(var i = 0; i < data.length; i++){
							if(data[i].type == type){
								gradeData = data[i].typeList;
							}
						}
						for(var i = 0; i < gradeData.length; i++){
							gradeOpt += "<option value='"+gradeData[i].grade+"' classcodes='"+gradeData[i].classcodes+"' classes='"+gradeData[i].classes+"'>"+gradeData[i].grade+"年级</option>";
						}
					}
					$("#student-grade").html(gradeOpt);
					
					var classOpt = "<option value='' classes=''>班级</option>";
					$("#student-classcode").html(classOpt);
				});
				
				$("#student-grade").unbind("change");
				$("#student-grade").change(function(){
					var classes = $("#student-grade option:selected").attr("classes").split(",");
					var classcodes = $("#student-grade option:selected").attr("classcodes").split(",");
					var classOpt = "";
					for(var j = 0; j < classes.length; j++){
						if(classcodes[j] == "" && classes[j] == ""){
							classOpt += "<option value='' classes=''>班级</option>";
						}else{
							classOpt += "<option value='"+classcodes[j]+"' classes='"+classes[j]+"'>"+classes[j]+"班</option>";
						}
					}
					$("#student-classcode").html(classOpt);
				});
			}else{
			
			}
			initStutent();
			
		},null,null);
	}
	
	function initStutent(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var userCode = $("#user-name",parent.parent.document).attr("code");
		var data = {
				cmd:"getStudentByUserCode",
				model:"user",
				userKey:userKey,
				data:{
					usercode:userCode,
				},
				remark:""
				
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				list = res.data;
				var index = $(".students-choice-select",parent.document).attr("ind");
				showData(list[index]);
			}
		},null,null);
	}
	
	function showData(data){
		for(var key in data){
			if($("#student-" + key)[0]){
				if($("#student-" + key)[0].tagName.toLocaleLowerCase() == "input"){
					if(key=="birdate"){
						$("#student-" + key).val(getSmpFormatDateByLong(data[key], "a"));
					}else if(key=="inschooldate"){
						$("#student-" + key).val(getSmpFormatDateByLong(data[key], "a"));
					}else{
						$("#student-" + key).val(data[key]);
					}
				}else if($("#student-" + key)[0].tagName.toLocaleLowerCase() == "select"){
					if(key=="periodtype"){
						var typeOpt = $("#student-periodtype option");
						for(var i = 0; i< typeOpt.length; i++){
							if(typeOpt[i].getAttribute("value").indexOf(data.periodtype) != -1){
								typeOpt[i].setAttribute("selected",true);
								$("#student-periodtype").change();
							}
						}
						
						var studentOptions = $("#student-grade option");
						for(var i = 0; i< studentOptions.length; i++){
							var classcode = data.classcode;
							if(studentOptions[i].getAttribute("classcodes").indexOf(classcode) != -1){
								studentOptions[i].setAttribute("selected",true);
								$("#student-grade").change();
							}
						}
						
						var classOptions = $("#student-classcode option");
						for(var i = 0; i< classOptions.length; i++){
							if( classOptions[i].getAttribute("value") == data.classcode){
								classOptions[i].selected = true;
							}
						}
					}else{
						var len = $("#student-" + key+" option");
						for(var i = 0; i < len.length ; i++){
							if(len[i].value == data[key]){
								len[i].selected = true;
							}
						}
					}
				}
			}
		}
	}
	
	function updateStudent(){
		var tstudent = getFormData();
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var obj = {};
		for(var key in tstudent){
			if(tstudent[key] != "" && tstudent[key] != undefined){
				obj[key] = tstudent[key];
			}
		}
		
		var data = {
				cmd:"update",
				model:"student",
				userKey:userKey,
				data:obj,
				remark:"",						
		};
		processBegin("正在保存中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("编辑学生信息成功");
				initStutent();
			}else{
				processError("编辑学生信息失败！");
			}
		},null,null);
	}
	
	
	function getFormData(){
		new setTStudent(null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
		var tstudent = getTStudent();
		var obj = {};
		for(var key in tstudent){
			if($("#student-"+key)[0]){
				if($("#student-"+key)[0].tagName.toLocaleLowerCase() == "input"){
					if(key=="birdate"){
						obj[key] = strToDate($("#student-"+key).val());
					}else if(key=="inschooldate"){
						obj[key] = strToDate($("#student-"+key).val());
					}else{
						obj[key] = encodeURI($("#student-"+key).val());
					}
				}else{
					if(key == "grade"){
						obj[key] = "";
					}else{
						obj[key] = encodeURI($("#student-"+key+" option:selected").val());
					}
				}
			}
		}
		obj.schoolid = "3446000056";
		new setTStudent(obj.tcard,obj.name,obj.birdate,obj.sex,obj.oldname,obj.country,obj.birplace,obj.nation,obj.political,obj.hukounature,obj.hukounature,obj.familyadress,obj.familyphone,obj.isonechild,obj.phone,obj.grade,obj.classcode,obj.studentcode,obj.inschooldate,obj.distance,obj.waytype,obj.inschooltype,obj.studytype,obj.fname1,obj.fphone1,obj.fcardtype1,obj.fcardid1,obj.fisguardian1,obj.faddress1,obj.frelation1,obj.fnation1,obj.fhukouplacecode1,obj.fcom1,obj.fjob1,obj.fremark1,obj.fname2,obj.fphone2,obj.fcardtype2,obj.fcardid2,obj.fisguardian2,obj.faddress2,obj.frelation2,obj.fnation2,obj.fhukouplacecode2,obj.fcom2,obj.fjob2,obj.fremark2,obj.imei,obj.cellphone,obj.cardcode,obj.schoolid);
		return getTStudent();	
	}
	
	//学生搜索年级班级联动
	function studentSeachMenu(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
				cmd:"getGradeAndClass",
				model:"tclass",
				userKey:userKey,
				data:{
					schoolcode:schoolcode
				},
				remark:"",						
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data; 
				studentSeachMenuCaches = data;
				var contents = "<option classcodes = '' value='班级'>年级</option>";
				for(var i = 0; i < data.length; i++){
					contents += "<option classcodes='"+data[i].classcodes+"' value='"+data[i].classes+"'>"+data[i].grade+"</option>";
				}
				$("#student-grade").empty();
				$("#student-grade").append(contents);
				$("#student-grade").unbind("change");
				$("#student-grade").change(function(){
					var classes = $("#student-grade option:selected").val();
					var classcodes = $("#student-grade option:selected").attr("classcodes");
					$("#student-classcode").empty();
					var classArr = classes.split(",");
					var classcodeArr = classcodes.split(",");
					var cont = "";
					for(var i = 0; i < classArr.length; i++){
						cont += "<option value='"+classcodeArr[i]+"'>"+classArr[i]+"</option>";
					}
					$("#student-classcode").append(cont);						
				});
			}else{
				var contents = "<option classcodes = '' value='班级'>年级</option>";
				$("#student-grade").empty();
				$("#student-grade").append(contents);
			}
		},null,null);
	}

	
	function check2(){
		var flg = false;
		var checkFails = $(".check-fail");
		for(var i = 0; i < checkFails.length; i++){
			if(checkFails[i].innerHTML != ""){
				flg = false;
				break;
			}else{
				flg = true;
			}
		}
		return flg;
	}
	
	function checkSave(){ 
			if($("#student-name").val() == ""){
				$("#student-name").focus();
				return false;
			}else if($("#student-birdate").val() == ""){
				$("#student-birdate").focus();
				return false;
			}else if($("#student-studentcode").val() == ""){
				$("#student-studentcode").focus();
				return false;
			}else if($("#student-inschooldate").val() == ""){
				$("#student-inschooldate").focus();
				return false;
			}else if($("#student-fname1").val() == ""){
				$("#student-fname1").focus();
				return false;			
			}else if($("#student-fphone1").val() == "" ){
				$("#student-fphone1").focus();
				return false;
			}else if($("#student-fcardid1").val() == ""){
				$("#student-fcardid1").focus();
				return false;
			}else{
				return true;
			}
	};
	
	function initbtns(){
		$(".students-choice",parent.document).unbind("click");
		$(".students-choice",parent.document).click(function(){
			$(".students-choice",parent.document).removeClass("students-choice-select");
			$(this,parent.document).addClass("students-choice-select");
			var index = $(".students-choice-select",parent.document).attr("ind");
			showData(list[index]);
		});	
		
		$(".save-btns").click(function(){
			if(checkSave()){
				if(check2()){
					updateStudent();
				}
			}
		});
	}
	
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();